package org.dromara.system.controller.xcx;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.common.web.core.BaseController;
import org.dromara.system.domain.bo.WbPowerCircleBo;
import org.dromara.system.domain.vo.SysUserVo;
import org.dromara.system.domain.vo.WbPowerCircleVo;
import org.dromara.system.domain.vo.WbProvinceVo;
import org.dromara.system.service.ISysUserService;
import org.dromara.system.service.IWbPowerCircleService;
import org.dromara.system.service.IWbProvinceService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.dromara.common.satoken.utils.LoginHelper;
import org.dromara.system.domain.vo.SysUserVo;

import java.util.Date;
import java.util.List;

/**
 * 电力圈增删改查
 *
 * @author Lion Li
 * @date 2024-05-07
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xcx/powerCircle")
public class CircleController extends BaseController {
    private final IWbPowerCircleService wbPowerCircleService;
    private final ISysUserService sysUserService;
    private final IWbProvinceService wbProvinceService;
//    /**
//     * 查询电力圈列表
//     */
//    @GetMapping("/listZYT")
//    public TableDataInfo<WbPowerCircleVo> listZYT(WbPowerCircleBo bo, PageQuery pageQuery) {
//
//            Long userId = LoginHelper.getUserId();
//            bo.setUserId(userId);
//            return wbPowerCircleService.queryPageListZYT(bo, userId,pageQuery);
//    }


    /**
     * 查询电力圈列表
     */
    @GetMapping("/list")
    public TableDataInfo<WbPowerCircleVo> list(WbPowerCircleBo bo, PageQuery pageQuery) {
        if (bo.getNewsType().equals("555") && LoginHelper.getLoginUser() != null) {
            Long userId = LoginHelper.getUserId();
            bo.setUserId(userId);
            return wbPowerCircleService.queryPageListZYT(userId,pageQuery);
        }
        bo.setStatus("1");
        return wbPowerCircleService.queryPageList(bo, pageQuery);
    }

    /**
     * 新增电力圈
     */
    @Log(title = "电力圈文章", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody WbPowerCircleBo bo ) {
        Long userId = LoginHelper.getLoginUser().getUserId();
        bo.setUserId(userId);
        SysUserVo sysUserVo = sysUserService.selectUserById(userId);
        //判断是否绑定企业 为空就是个人
        if (sysUserVo.getEnterpriseId() == null){
            bo.setReleaseType("0");
        }else {
            bo.setReleaseType("1");
        }
        bo.setReleaseTime(new Date());
        return toAjax(wbPowerCircleService.insertByBo(bo));
    }
    /**
     * 修改电力圈文章
     */
    @SaCheckPermission("system:powerCircle:edit")
    @Log(title = "电力圈文章修改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody WbPowerCircleBo bo) {
        return toAjax(wbPowerCircleService.updateByBo(bo));
    }

    /**
     * 删除电力圈
     *
     * @param circleIds 主键串
     */
    @Log(title = "电力圈增删改查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{circleIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] circleIds) {
        return toAjax(wbPowerCircleService.deleteWithValidByIds(List.of(circleIds), true));
    }

    /**
     * 获取电力圈详细信息
     *
     * @param circleId 主键
     */
    @GetMapping("/{circleId}")
    public R<WbPowerCircleVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long circleId) {
        WbPowerCircleVo wbPowerCircleVo = wbPowerCircleService.queryById(circleId);
        WbProvinceVo wbProvinceVo = wbProvinceService.queryById(Long.valueOf(wbPowerCircleVo.getLocation()));
        if(wbProvinceVo != null){
            wbPowerCircleVo.setProvinceName(wbProvinceVo.getProvinceName());
        }
        return R.ok(wbPowerCircleVo);
    }

}
